package com.oracle.lingshangejia.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.base.BaseActivity;
import com.oracle.lingshangejia.utils.AppManagerDelegate;
import com.oracle.lingshangejia.utils.SpUtils;
import com.lws.bugly.Bugly;

import org.lzh.framework.updatepluginlib.UpdateConfig;


public class SplashActivity extends BaseActivity {
    private int mark;
    @Override
    public int intiLayout() {
        return R.layout.activity_splash;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void initView() {
        mark= (int) SpUtils.get(SplashActivity.this,"mark",0);
        new Thread(){
            @Override
            public void run() {
                try {
                    boolean isFirist= (boolean) SpUtils.get(SplashActivity.this,"isFirst",false);
                    if (!isFirist) {
                        Intent it=new Intent(SplashActivity.this,GlideActivity.class);//启动GlideActivity
                        startActivity(it);
                        AppManagerDelegate.getInstance().finishActivity(SplashActivity.class);
                        //finish();//关闭当前活动
                    }else{
                        Thread.sleep(2000);
                        boolean isLogin= (boolean) SpUtils.get(SplashActivity.this,"isLogin",false);
                        if(isLogin){
                            if(mark==1){
                                Intent it=new Intent(SplashActivity.this, MainActivity.class);//启动MainActivity
                                startActivity(it);
                            }else{
                                int userid= (int) SpUtils.get(SplashActivity.this,"userid",0);
                                Intent it=new Intent(SplashActivity.this, ThreeDoctorMainActivity.class);//启动MainActivity
                                //it.putExtra("mark",2);
                                it.putExtra("ysid",userid);
                                startActivity(it);
                            }

                        }else{
                            Intent it=new Intent(SplashActivity.this,LoginActivity.class);//启动登录
                            startActivity(it);
                        }
                        AppManagerDelegate.getInstance().finishActivity(SplashActivity.class);
                       // finish();//关闭当前活动
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    @Override
    public void initData() {

    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    private void checkUpApp() {
        //检查权限（NEED_PERMISSION）是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权

        if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            //用户已经拒绝过一次，再次弹出权限申请对话框需要给用户一个解释
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission
                    .WRITE_EXTERNAL_STORAGE)) {
                Toast.makeText(this, "请开通相关权限，否则无法正常使用本应用！", Toast.LENGTH_SHORT).show();
            }
            //申请权限
            requestPermissions( new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 125);

        } else {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                //先获取是否有安装未知来源应用的权限
                boolean   haveInstallPermission = getPackageManager().canRequestPackageInstalls();
                if (! haveInstallPermission){
                     Toast.makeText(this, "请开通安装未知来源的权限，否则无法APP可能无法正常使用！", Toast.LENGTH_SHORT).show();
                }

            }


        }



    }





}
